package org.dromara.wmscloud.utils;

import com.itextpdf.kernel.color.Color;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.pdf.bean.PdfPrintData;
import org.dromara.common.pdf.utils.PDFUtils;
import org.dromara.wmscloud.bean.*;
import org.dromara.wmscloud.bean.typeEnum.InOrderTypeEnum;
import org.springframework.stereotype.Service;
import org.dromara.common.pdf.utils.QRUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PDFService {

    /**
     * 打印物料标签
     */
    public String printMatLabel(MatLabelPdfData matLabelPdfData,int num) throws Exception {
        if (num < 1) {
            num = 1;
        }

        List<PdfPrintData> pdfPrintDataList = new ArrayList<>();

        HashMap<String, String> map = new HashMap<>();
        map.put("supplierName", matLabelPdfData.getSupplierName());
        map.put("matCode", matLabelPdfData.getMatCode());
        map.put("matName", matLabelPdfData.getMatName());
        map.put("figNum", matLabelPdfData.getFigNum());
        map.put("quantity", String.valueOf(matLabelPdfData.getQuantity()));
        map.put("unitName", matLabelPdfData.getUnitCode());
        map.put("batch", matLabelPdfData.getBatch());
        map.put("orderNo", matLabelPdfData.getOrderNo());
        map.put("orderTypeLabel", matLabelPdfData.getOrderType());
        map.put("prodTime", DateUtils.formatDate(matLabelPdfData.getProdTime()));

        for (int i = 0; i < num; i ++){
            Map<String, String> map2 = new HashMap();
            map2.put("qr_af_image", QRUtils.qrout("LABEL:" + matLabelPdfData.getLabelId()));
            PdfPrintData pdfPrintData = new PdfPrintData();
            pdfPrintData.setField(map);
            pdfPrintData.setImage(map2);
            pdfPrintData.setTemplatePath("pdf/label.pdf");
            pdfPrintDataList.add(pdfPrintData);
        }
        return PDFUtils.pdfouts(pdfPrintDataList);
    }
    /**
     * 打印物料标签（批量）
     */
    public String printLabelBatch(List<MatLabelPdfData> pdfDataList) throws Exception {
        Map<String,String> map = null;
        List<PdfPrintData> printDataList = new ArrayList<>();
        for(MatLabelPdfData pdfData : pdfDataList){
            map = new HashMap<>();
            map.put("supplierName", pdfData.getSupplierName());
            map.put("matCode", pdfData.getMatCode());
            map.put("matName", pdfData.getMatName());
            map.put("figNum", pdfData.getFigNum());
            map.put("quantity", String.valueOf(pdfData.getQuantity()));
            map.put("unitName", pdfData.getUnitName());
            map.put("batch", pdfData.getBatch());
            map.put("orderNo", pdfData.getOrderNo());
            map.put("orderTypeLabel", pdfData.getOrderTypeLabel());
            map.put("prodTime", DateUtils.formatDate(pdfData.getProdTime()));

            Map<String, String> map2 = new HashMap();
            map2.put("qr_af_image", QRUtils.qrout("LABEL:" + pdfData.getLabelId()));
            PdfPrintData pdfPrintData = new PdfPrintData();
            pdfPrintData.setField(map);
            pdfPrintData.setImage(map2);
            pdfPrintData.setTemplatePath("pdf/label.pdf");

            printDataList.add(pdfPrintData);
        }
        return PDFUtils.pdfouts(printDataList);
    }
    /**
     * 打印仓库
     */
    public String printWareHouse(WarehousePdfData warehousePdfData) throws Exception {
        PdfPrintData pdfPrintData = new PdfPrintData();

        HashMap<String, String> map = new HashMap<>();
        map.put("warehouseCode", warehousePdfData.getWarehouseCode());
        map.put("warehouseName", warehousePdfData.getWarehouseName());
        pdfPrintData.setField(map);

        HashMap<String, String> img = new HashMap<>();
        img.put("qr_af_image", QRUtils.getQrCode("WAREHOUSE:" + warehousePdfData.getWarehouseCode()));
        pdfPrintData.setImage(img);
        pdfPrintData.setTemplatePath("pdf/warehouse.pdf");
        return PDFUtils.pdfout(pdfPrintData, Color.LIGHT_GRAY, Color.BLACK);
    }

    /**
     * 打印货位
     */
    public String printLocation(LocaltionPdfData location) throws Exception {
        PdfPrintData pdfPrintData = new PdfPrintData();
        Map<String, String> map = new HashMap<>();
        map.put("locationCode", location.getLocationCode());
        map.put("locationName", location.getLocationName());
        pdfPrintData.setField(map);

        Map<String, String> img = new HashMap<>();
        img.put("qr_af_image", QRUtils.getQrCode("LOCATION:" + location.getLocationCode()+ ":" + location.getWarehouseCode()));
        pdfPrintData.setImage(img);
        pdfPrintData.setTemplatePath("pdf/location.pdf");
        return PDFUtils.pdfout(pdfPrintData, Color.LIGHT_GRAY, Color.BLACK);
    }

    /**
     * 打印入库单
     */
    public String printInOrder(InOrderPdfData inOrder, List<InDetailPdfData> detailList) throws Exception {
        PdfPrintData pdfPrintData = new PdfPrintData();
        Map<String,String> map = new HashMap<>();
        map.put("orderNo", inOrder.getOrderNo());
        map.put("createTime", DateUtils.formatDate(inOrder.getCreateTime()));
        map.put("createBy", inOrder.getCreateBy());
        pdfPrintData.setField(map);

        if(!CollectionUtils.isEmpty(detailList)){
            String[][] data = null;
            pdfPrintData.setTitle(new String[]{"行号","物料编码","物料名称","财务编码","图号","数量","单位","单价","批次","供应商"});
            pdfPrintData.setColum(new float[]{1,2,3,2,2,1,1,1,3,3});
            data = new String[detailList.size() + 1][10];
            int i = 1;
            for(InDetailPdfData detail : detailList){
                data[i][0] = String.valueOf(i);
                data[i][1] = detail.getMatCode();
                data[i][2] = detail.getMatName();
                data[i][3] = detail.getFdCode();
                data[i][4] = detail.getFigNum();
                data[i][5] = String.valueOf(detail.getQuantity());
                data[i][6] = detail.getUnitName();
                data[i][7] = String.valueOf(detail.getUnitPrice());
                data[i][8] = detail.getBatch();
                data[i][9] = detail.getSupplierName();
                i++;
            }
            pdfPrintData.setData(data);
            pdfPrintData.setTable("table_af_image");
        }

        Map<String, String> img = new HashMap();
        img.put("qr_af_image", QRUtils.qrout("ORDER:" + inOrder.getOrderNo()));
        pdfPrintData.setImage(img);
        if(InOrderTypeEnum.PURCHASE.getValue().equals(inOrder.getOrderType())){
            pdfPrintData.setTemplatePath("pdf/purchase_in_order.pdf");
        }
        return PDFUtils.pdfout(pdfPrintData, Color.LIGHT_GRAY, Color.BLACK);
    }
}
